// miniprogram/pages/edit-duty/edit-duty.js
import Dialog from '../../vant-weapp/lib/dialog/dialog';
import  util  from '../../util.js'
const app = getApp();
const DialogTypes = { 
  edit: 'edit',
  add: 'add'
}
const dutyTransferUnit = {
  dateZeroPointTimestamp: null,
  content: null,
  userToken: null
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dutyList: [],
    duty_content: '',
    ifShowInputDialog: false,
    dialogType: DialogTypes.add,
    edit_duty_index: null
  },
  setEditDutyIndex(index) {
    this.setData({
      edit_duty_index: index
    })
  },
  getEditDutyIndex() {
    return this.data.edit_duty_index;
  },
  getDialogType() {
    return this.data.dialogType;
  },
  setDialogType(type) {
    this.setData({
      dialogType: type
    })
  },
  getDutyItem(index) {
    return this.data.dutyList[index];
  },
  setDutyList(newList) {
    this.setData({
      dutyList: newList
    })
  },
  delDuty(index) {
    wx.showToast({
      title: '请求中',
      icon: 'loading'
    })
    // 请求删除
    let dutyList = this.data.dutyList;
    let { _id } = dutyList[index];
    let { userToken } = dutyTransferUnit;
    wx.cloud.callFunction({
      name: 'delDuty',
      data: {
        _id,
        userToken
      }
    }).then(res => {
      if (res && res.result && res.result.msg === 'ok') {
        wx.showToast({
          title: '删除成功',
        });
        this.loadDutyList()
      }
    }, err => {
      console.log('删除失败', err)
    });
  },
  editDuty(index) {
    wx.showToast({
      title: '请求中',
      icon: 'loading'
    })
    let dutyItem = this.getDutyItem(index);
    // 请求编辑
    let dutyList = this.data.dutyList;
    dutyList[index].content = this.getDutyContent();
    let { _id, content } = dutyList[index];
    let { userToken } = dutyTransferUnit;
    wx.cloud.callFunction({
      name: 'editDuty',
      data: {
        _id,
        content,
        userToken
      }
    }).then(res => {
      if (res.result.msg === 'ok') {
        wx.showToast({
          title: '修改成功'
        })
        this.loadDutyList();
      }
    })
  },
  loadDutyList() {
   
    wx.cloud.callFunction({
      name: 'getDutyList',
      data: {
        userToken: dutyTransferUnit.userToken,
        createTimestamp: dutyTransferUnit.dateZeroPointTimestamp
      }
    }).then(res => {
      if (res && res.result && res.result.data) {
        this.setDutyList(res.result.data);
      }
    })
  },
  addDutyList(newDuty) {
    wx.showToast({
      title: '请求中',
      icon: 'loading'
    })
    let dutyList = this.data.dutyList;
    dutyList.push(newDuty);
    // 请求添加
    wx.cloud.callFunction({
      name: 'addDuty',
      data: {
        userToken: dutyTransferUnit.userToken,
        content: dutyTransferUnit.content,
        createTimestamp: dutyTransferUnit.dateZeroPointTimestamp
      }
    }).then(data => {
      if (data && data.result && data.result.status === 0) {
        wx.showToast({
          title: '添加成功',
        })
        this.loadDutyList();
      };
    }, err => {
      console.log('添加失败', err);
    });
    
  },
  setDutyContent(new_value) {
    dutyTransferUnit.content = new_value;
    this.setData({
      duty_content: new_value
    })
  },
  getDutyContent() {
    return this.data.duty_content;
  },
  showInputDialog() {
    this.setDutyContent('');
    this.setData({
      ifShowInputDialog: true
    })
    return new Promise((resolve, reject) => {
      this.nextTick().then(() => {
        resolve();
      });
    });
  },
  handleEditDuty(e) {
    const index = e.target.dataset.index;
    const dutyList = this.data.dutyList;
    const duty = dutyList[index].content;
    this.setDialogType(DialogTypes.edit);
    this.setEditDutyIndex(index);
    this.showInputDialog().then(() => {
      this.setDutyContent(duty);
    });
    
  },
  handleDeleteDuty(e) {
    wx.showModal({
      title: '友情提示',
      content: '确认删除?',
      success:(res) => {
        if(res.confirm) {
          const index = e.target.dataset.index;
          this.delDuty(index);
        }
      }
    })
    
  },
  handleConfirm() {
    let duty_content = this.getDutyContent();
    if (duty_content.length === 0) {
      wx.showToast({
        title: '输入不能为空',
        icon: 'none'
      })
      return;
    }
    let newDuty = {
      content: duty_content
    };
    // add or edit ?
    const dialogType = this.getDialogType();
    if (dialogType === DialogTypes.add) {
      // 添加
      this.addDutyList(newDuty);
    } else {
      // 编辑
      let edit_duty_index = this.getEditDutyIndex();
      this.editDuty(edit_duty_index);
    }
    
  },
  handleClickAdd() {
    this.setDialogType(DialogTypes.add);
    this.showInputDialog();
  },
  handleInputDuty(e) {
    let duty_content = e.detail;
    this.setDutyContent(duty_content)
  },
  nextTick() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve();
      }, 0);
    });
  },
  handleInputDialogClose() {
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.cloud.init();
    const dateZeroPointTimestamp = options.dateZeroPointTimestamp;
    dutyTransferUnit.dateZeroPointTimestamp = dateZeroPointTimestamp;
    dutyTransferUnit.userToken = app.globalData.getUserToken();
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    this.loadDutyList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})